package cn.xiaopengstack.test;

import cn.xiaopengstack.config.RestClientFilter;
import io.modelcontextprotocol.client.McpSyncClient;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.mcp.SyncMcpToolCallbackProvider;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.web.client.RestClient;

import java.util.Arrays;
import java.util.Collections;

public class BaseTet {


    protected OpenAiApi deekseekOpenapi = OpenAiApi.builder()
            .baseUrl("https://api.deepseek.com/")
            .completionsPath("v1/chat/completions")
            .embeddingsPath("v1/embeddings")
            .restClientBuilder(RestClient.builder().requestInterceptor(new RestClientFilter()))
            .apiKey("sk-529fb4e1f9e448b6bbe2a252a0f5c796")
            .build();

    protected OpenAiChatModel deepseekChatModel = OpenAiChatModel.builder()
            .openAiApi(deekseekOpenapi)
            .defaultOptions(OpenAiChatOptions.builder()
                    .model("deepseek-chat")
                    .build())
            .build();



    public ChatClient customerClient(OpenAiChatModel openAiChatModel, McpSyncClient... mcpSyncClients){
        ChatClient.Builder builder = ChatClient.builder(openAiChatModel)
                .defaultAdvisors(new SimpleLoggerAdvisor());

        if (CollectionUtils.isEmpty(Arrays.asList(mcpSyncClients))) {
            return builder.build();
        } else {
            return builder.defaultToolCallbacks(new SyncMcpToolCallbackProvider(mcpSyncClients).getToolCallbacks()).build();
        }
    }

}
